﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Schedule
{
    /// <summary>
    /// CalendarList 的摘要说明。
    /// </summary>
    public partial class CalendarViewPage : Page
    {
        protected string Action;
        protected int displayType = 1;
        protected string Username;

        private void Page_Load(object sender, EventArgs e)
        {
            Username = SiteContext.Current.UserName;

            if (!Page.IsPostBack)
            {
                Action = Request.QueryString["Action"] != null ? Request.QueryString["Action"] : "1";
                tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton2.gif");
                lnkbtnNew.Attributes["onclick"] = "var newwin=window.open('Manage.aspx','newtask','toolbar=yes,scrollbars=yes,width=800,height=600,resizable=yes');newwin.moveTo(0,0);newwin.focus();";
                if (Action == "1")
                {
                    var task = new Task();
                    dgList.Visible = true;
                    lnkbtnAccept.Visible = true;
                    lnkbtnCancel.Visible = true;
                    lnkbtnFinish.Visible = true;
                    lnkbtnAdd.Visible = true;
                    //this.lnkbtnAdd.Attributes.Add("onclick","return dialwinprocess('"+DateTime.Today.ToShortDateString()+"','8','1','0')");
                    //this.lnkbtnAdd.Attributes.Add("onclick","self.location='CalendarView.aspx?Action=1';return true;");
                    DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, 1));
                    dgList.DataSource = mydb.DefaultView;
                    dgList.DataBind();
                    setgrid();
                    mydb.Dispose();
                }

                if (Action == "2")
                {
                    Response.Write("<table width=\"49%\" border=\"1\" align=\"center\" class=\"top\"><tr><td width=\"2%\"  bgcolor=\"#A692F5\">&nbsp;</td>	<td width=\"18%\">未确认任务</td> <td width=\"2%\" bgcolor=\"BlanchedAlmond\">&nbsp;</td> <td width=\"18%\">已确认任务</td></tr></table>");
                    dgList.Visible = false;
                    lnkbtnAccept.Visible = false;
                    lnkbtnCancel.Visible = false;
                    lnkbtnFinish.Visible = false;
                    lnkbtnAdd.Visible = false;
                    lnkbtnToday.Visible = false;
                    lnkbtnHistory.Visible = false;
                    lnkbtnFinished.Visible = false;
                    tblContainer.Rows[0].Cells[1].Attributes.Add("background", "");
                    tblContainer.Rows[0].Cells[0].Attributes.Add("background", "");
                    tblContainer.Rows[0].Cells[2].Attributes.Add("background", "");
                    DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek = DayOfWeek.Monday;
                    DateTimeFormatInfo.CurrentInfo.DayNames = new[] { "七", "一", "二", "三", "四", "五", "六" };
                    PopulateListView();
                    PopulateDateToTable(DateTime.Today.AddDays(-(Convert.ToInt32(DateTime.Today.DayOfWeek)) + 1), 6);
                    lnkbtnReturn.Visible = true;
                }
                //Response.Write(DateTime.Today.AddDays(-(Convert.ToInt32(DateTime.Today.DayOfWeek))+1).ToShortDateString()); 
            }
            //Response.Write(Calendar1.SelectedDate .ToShortDateString());
        }

        public void PopulateDateToTable(DateTime sDate, int dayCount)
        {
            if (listStaff.SelectedIndex != 0)
            {
                Username = listStaff.SelectedItem.Value;
            }

            var task = new Task();
            var TaskData = new int[5][];

            // 初始化天数据至ArrayList
            //			ArrayList demoDayData = new ArrayList();	
            //			demoDayData.Add(9);
            //			demoDayData.Add(10);
            //			demoDayData.Add(11);
            //			demoDayData.Add(12);


            //			int[][] TaskData = new int[5][];  //每日的任务数据
            //			TaskData[0] = new int[]{0,0,0,0,0,13,14,15,16,17,18};
            //			TaskData[1] = new int[]{8,9,10,11,0,0,0,0,0,0,0};
            //			TaskData[2] = new int[]{0,9,10,11,12,13,0,0,0,0,0};
            //			TaskData[3] = new int[]{8,9,10,11,12,13,14,15,0,0,0};
            //			TaskData[4] = new int[]{0,0,0,0,0,0,0,0,0,17,18};
            //			demoData[5] = new int[]{0,0,0,0,0,0,0,0};
            //			demoData[6] = new int[]{0,0,0,0,0,0,0,0};
            //			demoData[7] = new int[]{0,0,0,0,0,0,0,0};
            // 生成行和单元格
            var DaySch = new ArrayList(dayCount + 1); //根据选择的天数生成数组 加1是因为会增加一信息列
            var DayTask = new ArrayList(dayCount + 1); //记录每天的前五条任务ID
            DaySch.Add(new int[5][]); //加入一个废列
            DayTask.Add(new String[5]);
            for (int p = 0; p < dayCount; p++)
            {
                TaskData = new int[5][];
                string tmpDate = sDate.AddDays(p).ToString();
                string[] a = task.GetTop5DayTaskList(tmpDate, Username);

                //	TaskData[0] = new int[]{0,0,0,0,0,13,14,15,16,17,18};
                //				TaskData[1] = new int[]{8,9,10,11,0,0,0,0,0,0,0};
                //				TaskData[2] = new int[]{0,9,10,11,12,13,0,0,0,0,0};
                //				TaskData[3] = new int[]{8,9,10,11,12,13,14,15,0,0,0};
                //				TaskData[4] = new int[]{0,0,0,0,0,0,0,0,0,17,18};
                for (int k = 0; k < a.Length; k++)
                {
                    TaskData[k] = task.GetTaskPeriod(tmpDate, Username, Int32.Parse(a[k]));
                }
                DaySch.Add(TaskData);
                DayTask.Add(a);
            }


            int numcells = dayCount;
            const int startTimeNo = 8;
            SqlDataReader dataReader = task.GetPeriodInfo();
            var period = new ArrayList();
            while (dataReader.Read())
            {
                period.Add(dataReader[1].ToString());
            }
            int numrows = period.Count;


            //  初始化表头

            #region 初始化表头

            var r = new TableRow();
            TableCell c;
            for (int i = 0; i < numcells + 1; i++)
            {
                string s = sDate.AddDays(i - 1).ToShortDateString() == DateTime.Today.ToShortDateString() ? "<font color=red><b>" + DateTime.Today.ToShortDateString() + "</b></font>" : sDate.AddDays(i - 1).ToShortDateString();

                c = new TableCell();
                c.CssClass = "top";
                c.Controls.Add(new LiteralControl((i == 0) ? "&nbsp;&nbsp; 时段&nbsp;&nbsp;&nbsp;    " : s + "&nbsp;&nbsp;&nbsp;" + Tools.ConvertDayOfWeekToZh(sDate.AddDays(i - 1).DayOfWeek)));
                r.Cells.Add(c);
            }

            #endregion

            //			Table1.CellPadding = 0;
            //			Table1.CellSpacing = 0;
            Table1.Rows.Add(r);
            // 初始化表头结束

            // 根据时间段共生成8行
            for (int j = 0; j < numrows; j++)
            {
                r = new TableRow();

                // 每行根据选择的天数生成列
                for (int i = 0; i < dayCount + 1; i++)
                {
                    c = new TableCell();

                    Table newtable;

                    if (i == 0)
                    {
                        if (j % 2 == 0)
                        {
                            string[] a = period[j].ToString().Split('-');
                            c.Controls.Add(new LiteralControl("<div style='position:absolute; width:86px; height:76px; z-index:1'><font size=5>" + a[0] + "</font></div>"));
                        }
                        else
                        {
                            c.Controls.Add(new LiteralControl("-"));
                        }
                    }
                    else
                    {
                        //如果不是第一列

                        //					newtable.BorderWidth = 0;
                        //newtable.GridLines = System.Web.UI.WebControls .GridLines.Vertical;
                        // 在某列中新table设置开始
                        newtable = new Table();
                        var nr = new TableRow();

                        #region 颜色代码

                        var colorlist = new ArrayList(5);
                        string[] colorArray = task.GetTop5ConfirmedTaskList(sDate.AddDays(i - 1).ToShortDateString(), Username);
                        for (int p = 0; p < colorArray.Length; p++)
                        {
                            //				int ca = ro.Next(0,255);
                            //				int cb = ro.Next(0,255);
                            //				int cc = ro.Next(0,255);
                            //				colorlist.Add(System.Drawing.Color.FromArgb(ca,cb,cc));
                            if (colorArray[p] == "0")
                            {
                                colorlist.Add(Color.FromName("#A692F5"));
                            }
                            else
                            {
                                colorlist.Add(Color.BlanchedAlmond);
                            }
                        }

                        #endregion

                        for (int q = 0; q < TaskData.Length; q++)
                        {
                            var tmp = (int[][])DaySch[i];
                            var a = (String[])DayTask[i];
                            var nc = new TableCell();


                            if (Int32.Parse(tmp[q][j].ToString()) != 0)
                            {
                                nc.Controls.Add(new LiteralControl("*"));
                                nc.BackColor = (Color)colorlist[q];
                                nc.Attributes.Add("onclick", "return dialwinprocess('" + sDate.AddDays(i - 1).ToShortDateString() + "','" + (startTimeNo + j) + "','2','" + a[q] + "')");
                                nc.Style.Add("cursor", "hand");
                            }
                            else
                            {
                                nc.Controls.Add(new LiteralControl("<font color=#FFF8F7>0</font>"));
                                nc.BackColor = Color.FromName("#FFF8F7");
                                nc.Attributes.Add("onclick", "return dialwinprocess('" + sDate.AddDays(i - 1).ToShortDateString() + "','" + (startTimeNo + j) + "','1','0')");
                                nc.Attributes.Add("OnMouseOver", "return high( this );");
                                nc.Attributes.Add("OnMouseOut", "return low( this );");
                                //nc.Attributes.Add("onclick","return dialwinprocess('"+q.ToString()+j.ToString()+tmp[q][j].ToString()+"')");
                                nc.Style.Add("cursor", "hand");
                                //							System.Drawing.ColorConverter cc=new System.Drawing.ColorConverter();
                                //							newtable.BackColor=(System.Drawing.Color)cc.ConvertFromString("#FF9900");
                                //						
                            }
                            nr.Cells.Add(nc);
                        }


                        newtable.CellPadding = 1;
                        newtable.CellSpacing = 1;
                        newtable.GridLines = GridLines.Both;
                        newtable.BorderWidth = 0;
                        newtable.Rows.Add(nr);
                        // 新table设置结束

                        c.Controls.Add(newtable);
                    }

                    r.Cells.Add(c);
                }
                Table1.CellPadding = 1;
                Table1.CellSpacing = 1;
                Table1.GridLines = GridLines.Horizontal;
                Table1.BorderWidth = 1;
                Table1.Rows.Add(r);
            }
        }

        public string GetStatus(string str)
        {
            switch (str)
            {
                case "0":
                    return "?"; // 待定
                case "1":
                    return "!"; // 待办
                case "2":
                    return "√"; // 完成
                default:
                    return "";
            }
        }

        public string GetPeriodByPeriodID(string begintime, string endtime)
        {
            int b = Int32.Parse(begintime);
            int e = Int32.Parse(endtime);
            var dt = new DateTime(1999, 1, 1, 8, 0, 0, 0);
            var ts = new TimeSpan(0, 0, (b - 1) * 30, 0, 0);
            DateTime bt = dt.Add(ts);
            DateTime et = bt.Add(new TimeSpan(0, 0, (e - b + 1) * 30, 0, 0));
            return bt.ToShortTimeString() + "----" + et.ToShortTimeString();
        }


        private void setgrid()
        {
            foreach (DataGridItem dgi in dgList.Items)
            {
                var lb = (Label)(dgi.Cells[4].Controls[1]);
                if (lb.Text == "?")
                {
                    dgi.BackColor = Color.AliceBlue;
                }
            }
        }

#if false
        private void btnChart_Click(object sender, EventArgs e)
        {
            //　打印图示代码
            Response.Write("<table width=\"49%\" border=\"1\" align=\"center\" class=\"top\"><tr><td width=\"2%\"  bgcolor=\"#A692F5\">&nbsp;</td>	<td width=\"18%\">未确认任务</td> <td width=\"2%\" bgcolor=\"BlanchedAlmond\">&nbsp;</td> <td width=\"18%\">已确认任务</td></tr></table>");
            dgList.Visible = false;
            lnkbtnAccept.Visible = false;
            lnkbtnCancel.Visible = false;
            lnkbtnFinish.Visible = false;
            lnkbtnAdd.Visible = false;
            DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek = DayOfWeek.Monday;
            DateTimeFormatInfo.CurrentInfo.DayNames = new[]{"七", "一", "二", "三", "四", "五", "六"};
            PopulateListView();
            PopulateDateToTable(DateTime.Today.AddDays(-(Convert.ToInt32(DateTime.Today.DayOfWeek)) + 1), 6);
        }


        private void btnList_Click(object sender, EventArgs e)
        {
            var task = new Task();
            dgList.Visible = true;
            lnkbtnCancel.Visible = true;
            lnkbtnFinish.Visible = true;
            lnkbtnAccept.Visible = true;
            lnkbtnAdd.Visible = true;
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, 1));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
        }
#endif

        private void lnkbtnAccept_Click(object sender, EventArgs e)
        {
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 1, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='CalendarView.aspx?Action=1';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void lnkbtnCancel_Click(object sender, EventArgs e)
        {
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 0, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='CalendarView.aspx?Action=1';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void lnkbtnFinish_Click(object sender, EventArgs e)
        {
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 2, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='CalendarView.aspx?Action=1';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void lnkbtnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("CalendarView.aspx?Action=2");
        }

        private void lnkbtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("CalendarView.aspx?Action=1");
        }

        private void lnkbtnToday_Click(object sender, EventArgs e)
        {
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, 1));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
            displayType = 1;
        }

        private void lnkbtnHistory_Click(object sender, EventArgs e)
        {
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, 2));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
            displayType = 2;
        }

        private void lnkbtnFinished_Click(object sender, EventArgs e)
        {
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, 3));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            //	setgrid();
            mydb.Dispose();
            displayType = 3;
        }

        private void lnkbtnNew_Click(object sender, EventArgs e)
        {
            //Response.Redirect("Manage.aspx");
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgList.CurrentPageIndex = e.NewPageIndex;
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lnkbtnToday.Click += new System.EventHandler(this.lnkbtnToday_Click);
            this.lnkbtnHistory.Click += new System.EventHandler(this.lnkbtnHistory_Click);
            this.lnkbtnFinished.Click += new System.EventHandler(this.lnkbtnFinished_Click);
            this.lnkbtnReturn.Click += new System.EventHandler(this.lnkbtnReturn_Click);
            this.lnkbtnNew.Click += new System.EventHandler(this.lnkbtnNew_Click);
            this.lnkbtnAccept.Click += new System.EventHandler(this.lnkbtnAccept_Click);
            this.lnkbtnCancel.Click += new System.EventHandler(this.lnkbtnCancel_Click);
            this.lnkbtnFinish.Click += new System.EventHandler(this.lnkbtnFinish_Click);
            this.lnkbtnAdd.Click += new System.EventHandler(this.lnkbtnAdd_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        #region 初始化下拉列表框

        /// <summary>
        /// 对下拉列表进行初始化
        /// </summary>
        private void PopulateListView()
        {
            var staffList = Staff.GetStaffFromPosition(Username);
            listStaff.Items.Clear();
            foreach (var staff in staffList)
            {
                listStaff.Items.Add(new ListItem(staff.RealName, staff.Name));
            }

            listStaff.Items.Insert(0, new ListItem("本人的日程记录", "0"));
            listStaff.SelectedIndex = 0;
        }

        #endregion

        #region Nested type: demodat

        public class demodat
        {
            public int StartTime
            {
                //文件ID
                get;
                set;
            }

            public int EndTime
            {
                //文档ID
                get;
                set;
            }
        }

        #endregion
    }
}